package entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name = "notification")
@NamedQueries({
    @NamedQuery(name = "Notification.findAll", query = "SELECT n FROM Notification n"),
    @NamedQuery(name = "Notification.findByEventId", query = "SELECT n FROM Notification n WHERE n.notificationPK.eventId = :eventId"),
    @NamedQuery(name = "Notification.findByParticipantUsername", query = "SELECT n FROM Notification n WHERE n.notificationPK.participantUsername = :participantUsername"),
    @NamedQuery(name = "Notification.findByTimestamp", query = "SELECT n FROM Notification n WHERE n.notificationPK.timestamp = :timestamp"),
    @NamedQuery(name = "Notification.findByTitle", query = "SELECT n FROM Notification n WHERE n.title = :title"),
    @NamedQuery(name = "Notification.findByNotificationType", query = "SELECT n FROM Notification n WHERE n.notificationType = :notificationType")})
public class Notification implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @EmbeddedId
    protected NotificationPK notificationPK;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "title")
    private String title;
    
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(name = "text")
    private String text;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "notificationType")
    private String notificationType;
    
    @JoinColumn(name = "eventId", referencedColumnName = "eventId", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Event event;
    
    @JoinColumn(name = "participantUsername", referencedColumnName = "username", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private User participant;

    public Notification() {
    }

    public Notification(NotificationPK notificationPK) {
        this.notificationPK = notificationPK;
    }

    public Notification(NotificationPK notificationPK, String title, String text, String notificationType) {
        this.notificationPK = notificationPK;
        this.title = title;
        this.text = text;
        this.notificationType = notificationType;
    }

    public Notification(int eventId, String participantUsername, Date timestamp) {
        this.notificationPK = new NotificationPK(eventId, participantUsername, timestamp);
    }

    public NotificationPK getNotificationPK() {
        return notificationPK;
    }

    public void setNotificationPK(NotificationPK notificationPK) {
        this.notificationPK = notificationPK;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getNotificationType() {
        return notificationType;
    }

    public void setNotificationType(String notificationType) {
        this.notificationType = notificationType;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public User getParticipant() {
        return participant;
    }

    public void setParticipant(User participant) {
        this.participant = participant;
    }
    
    public boolean isDeletion() {
        return NotificationType.DELETION.toString().equalsIgnoreCase(notificationType);
    }
    
    public boolean isModification() {
        return NotificationType.MODIFICATION.toString().equalsIgnoreCase(notificationType);
    }
    
    public boolean hasEvent() {
        return event != null;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (notificationPK != null ? notificationPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Notification)) {
            return false;
        }
        Notification other = (Notification) object;
        return Objects.equals(this.notificationPK, other.notificationPK);
    }
    
    @Override
    public String toString() {
        return "entity.Notification[ notificationPK=" + notificationPK + " ]";
    }
    
}
